<template>
  <div class="home-product" ref="target">
    <HomePannel :title="cate.name" v-for="cate in products" :key="cate.id">
      <template v-slot:right>
        <div class="sub">
          <router-link v-for="sub in cate.children" :key="sub.id" to="/">{{ sub.name }}</router-link>
        </div>
        <XtxMore :path="`/category/${cate.id}`" />
      </template>
      <div class="box">
        <RouterLink to="/" class="cover">
          <img v-lazy="cate.picture" alt="" />
          <strong class="label">
            <span>{{ cate.name }}</span>
            <span>{{ cate.saleInfo }}</span>
          </strong>
        </RouterLink>
        <ul class="goods-list">
          <li v-for="item in cate.goods" :key="item.id">
            <HomeGoods :goods="item" />
          </li>
        </ul>
      </div>
    </HomePannel>
  </div>
</template>

<script>
import HomePannel from './home-pannel.vue'
import HomeGoods from './home-goods.vue'
import { findGoods } from '@/api/home'
import { useLazyData } from '@/hooks/index'

export default {
  name: 'HomeProduct',
  components: { HomePannel, HomeGoods },
  setup() {
    const { result, target } = useLazyData(findGoods)
    return { products: result, target }
  }
}
</script>

<style lang="less" scoped>
.home-product {
  background-color: #fff;
  height: 2900px;
  .sub {
    margin-bottom: 2px;
    a {
      padding: 2px 12px;
      font-size: 16px;
      border-radius: 4px;
      &:hover {
        color: #fff;
        background-color: @xtxColor;
      }
      &:last-child {
        margin-right: 80px;
      }
    }
  }
  .box {
    display: flex;
    .cover {
      width: 240px;
      height: 610px;
      margin-right: 10px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
      .label {
        width: 188px;
        height: 66px;
        display: flex;
        font-size: 18px;
        color: #fff;
        line-height: 66px;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        span {
          text-align: center;
          &:first-child {
            width: 76px;
            background: rgba(0, 0, 0, 0.9);
          }
          &:last-child {
            flex: 1;
            background: rgba(0, 0, 0, 0.7);
          }
        }
      }
    }
    .goods-list {
      width: 990px;
      display: flex;
      flex-wrap: wrap;
      li {
        width: 240px;
        height: 300px;
        margin-right: 10px;
        margin-bottom: 10px;
        &:nth-last-child(-n + 4) {
          margin-bottom: 0;
        }
        &:nth-last-child(4n) {
          margin-right: 0px;
        }
      }
    }
  }
}
</style>
